﻿using Donal.Xms.Common;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Donal.Xms.Web.qianyi.dialog
{
    public partial class dialog_address_edit : Web.UI.ManagePage
    {        
        protected string action = DTEnums.ActionEnum.Add.ToString(); //操作类型
        private int id = 0;
        protected int parent_id = 0;
        protected string company_name = string.Empty;
        protected string area = string.Empty;

        protected void Page_Load(object sender, EventArgs e)
        {
            string _action = DTRequest.GetQueryString("action");
            this.id = DTRequest.GetQueryInt("id");

            if (!string.IsNullOrEmpty(_action) && _action == DTEnums.ActionEnum.Edit.ToString())
            {
                this.action = DTEnums.ActionEnum.Edit.ToString();//修改类型

                if (this.id == 0)
                {
                    JscriptMsg("传输参数不正确！", "back");
                    return;
                }
                if (!new BLL.user_addr_book().Exists(this.id))
                {
                    JscriptMsg("地址信息不存在或已被删除！", "back");
                    return;
                }
            }
            if (!Page.IsPostBack)
            {
                ChkAdminLevel("user_company", DTEnums.ActionEnum.Edit.ToString()); //检查权限

                if (action == DTEnums.ActionEnum.Edit.ToString()) //修改
                {
                    ShowInfo(this.id);
                }
                else if (action == DTEnums.ActionEnum.Add.ToString())
                {
                    Model.company company = new BLL.company().GetModel(id);
                    hidParentId.Value = id.ToString();
                    company_name = company.companyName;
                }
            }
        }

        #region 赋值操作=================================
        private void ShowInfo(int _id)
        {
            BLL.user_addr_book bll = new BLL.user_addr_book();
            Model.user_addr_book model = bll.GetModel(_id);

            parent_id = model.user_id;
            hidParentId.Value = model.user_id.ToString();

            Model.company company = new BLL.company().GetModel(parent_id);
            company_name = company.companyName;
            hidCompanyName.Value= company.companyName;

            rbtGroup.SelectedValue = model.is_default.ToString();
            txtUserName.Text = model.accept_name;
            txtMobile.Text = model.mobile;
            txtTelphone.Text = model.telphone;
            txtEmail.Text = model.email;
            area = model.area;
            hidArea.Value = area.Split(',')[2];
            hidCity.Value = area.Split(',')[1];
            hidProvince.Value = area.Split(',')[0];
            txtAddress.Text = model.address;
            txtPostCode.Text = model.post_code;

        }
        #endregion

        #region 增加操作=================================
        private bool DoAdd()
        {
            bool result = false;
            Model.user_addr_book model = new Model.user_addr_book();
            BLL.user_addr_book bll = new BLL.user_addr_book();

            model.user_id = int.Parse(hidParentId.Value);

            model.user_name = hidCompanyName.Value;

            model.is_default = int.Parse(rbtGroup.SelectedValue);
            model.accept_name = txtUserName.Text;
            model.mobile = txtMobile.Text;
            model.telphone = txtTelphone.Text;
            model.email = txtEmail.Text;
            model.area = hidProvince.Value + "," + hidCity.Value + "," + hidArea.Value;
            model.address = txtAddress.Text;
            model.post_code = txtPostCode.Text;
            model.add_time = DateTime.Now;

            if (bll.Add(model))
            {
                AddAdminLog(DTEnums.ActionEnum.Add.ToString(), "添加地址 " + JsonConvert.SerializeObject(model)); //记录日志
                result = true;
            }
            return result;
        }
        #endregion

        #region 修改操作=================================
        private bool DoEdit(int _id)
        {
            BLL.user_addr_book bll = new BLL.user_addr_book();
            bool result = false;
            Model.user_addr_book model = bll.GetModel(_id);

            model.user_id = int.Parse(hidParentId.Value);

            model.user_name = hidCompanyName.Value;

            model.is_default = int.Parse(rbtGroup.SelectedValue);
            model.accept_name = txtUserName.Text;
            model.mobile = txtMobile.Text;
            model.telphone = txtTelphone.Text;
            model.email = txtEmail.Text;
            model.area = hidProvince.Value + "," + hidCity.Value + "," + hidArea.Value;
            model.address = txtAddress.Text;
            model.post_code = txtPostCode.Text;

            if (bll.Update(model))
            {
                AddAdminLog(DTEnums.ActionEnum.Edit.ToString(), "修改地址信息 " + JsonConvert.SerializeObject(model)); //记录日志
                result = true;
            }
            return result;
        }
        #endregion

        //保存
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            if (action == DTEnums.ActionEnum.Edit.ToString()) //修改
            {
                ChkAdminLevel("user_company", DTEnums.ActionEnum.Edit.ToString()); //检查权限
                if (!DoEdit(this.id))
                {
                    JscriptMsg("保存过程中发生错误！", "");
                    return;
                }
                JscriptMsg("修改地址成功！", "../common/company_edit.aspx?id=" + hidParentId.Value + "&action=" + DTEnums.ActionEnum.Edit + "&pageindex=1");
            }
            else //添加
            {
                ChkAdminLevel("user_company", DTEnums.ActionEnum.Add.ToString()); //检查权限
                if (!DoAdd())
                {
                    JscriptMsg("保存过程中发生错误！", "");
                    return;
                }
                JscriptMsg("添加地址成功！", "../common/company_edit.aspx?id=" + hidParentId.Value + "&action=" + DTEnums.ActionEnum.Edit + "&pageindex=1");
            }
        }


    }
}